function Lpad(str, len)
{
 str = str + "";
 while(str.length < len)
 {
  str = "0"+str;
 }
 return str;
}
/*
* 　　　　G calendar v0.7
* (공휴일 및 국경일 체크기능없음)
*
*　　　moidfied by 곰탱.
*/

var action_global = "{year}-{month}-{day}"; //action의 전역변수

function cal(year, month, day, obj, isOnOff){


	var i, j, day_num=1;
	var last_week, month_link="";
	var calDate = new Date(year, month-1, day_num);
	var checkDate = new Date();
	var date_array = new Array(6);

	var obj_name = "document.all." + obj.name;
	eval(obj_name).blur();	

	for(i=0;i<6;i++) date_array[i] = new Array("","","","","","","");

	//스크립트 추출 및 변환, 내용이 없으면 마지막 action 사용
	var script = action_global;
	

	for(j=0;j<6;j++){
		for(i=0;i<7;i++){
			if(j==0 && i==0)
				i = calDate.getDay();
			date_array[j][i]=day_num+"";
			day_num++;

			//해당 날짜값에 대한 유효성 검사
			checkDate.setFullYear(year, month-1, day_num);
			if(checkDate.getDate() != day_num) break;
		}
		if(checkDate.getDate() != day_num) break;
	}
	last_week = (date_array[5][0])?6:5;

	outStr = "<table bgcolor=#CCCCCC  width=160  border=0 cellpadding=0 cellspacing=0 ><tr><td>\n";
	outStr += "<table width=100% height=100% border=0 cellpadding=0 cellspacing=1 >\n";
	outStr += "<col width=20 align=center></col><col width=20 align=center></col><col width=20 align=center></col><col width=20 align=center></col><col width=20 align=center></col><col width=20 align=center></col><col width=20 align=center></col>\n";
	outStr += "<tr><td class=calendar style='color:red'>S<td class=calendar>M<td class=calendar>T<td class=calendar>W<td class=calendar>T<td class=calendar>F<td class=calendar style='color:blue'>S</tr>\n";

	var value_script = "";

	for(j=0;j<last_week;j++){
		outStr += "<tr>";
		for(i=0;i<7;i++){
			//스크립트 자료변환
			if(date_array[j][i]!=""){
				value_script = script.replace("{year}", year);
				value_script = value_script.replace("{month}",Lpad(month,2));
				value_script = value_script.replace("{day}", Lpad(date_array[j][i],2));
			}
			else 
				value_script = "";

			//날짜별로 스크립트 출력
			if(i==0){ 
				outStr += "<td class=calendar style='color:red;cursor:hand' onMouseover=this.style.backgroundColor='#FFEEEE' onMouseout=this.style.backgroundColor='' onClick=\""+obj_name+".value='"+value_script+"';onoff_Gcal() \">"+date_array[j][i]+"</td>"; continue; 
			}
			if(i==6){ 
				outStr += "<td class=calendar style='color:blue;cursor:hand' onMouseover=this.style.backgroundColor='#EEEEFF' onMouseout=this.style.backgroundColor='' onClick=\""+obj_name+".value='"+value_script+"';onoff_Gcal() \">"+date_array[j][i]+"</td>"; continue; 
			}
			outStr += "<td class=calendar style='cursor:hand' onMouseover=this.style.backgroundColor='#EEEEEE' onMouseout=this.style.backgroundColor='' onClick=\""+obj_name+".value='"+value_script+"';onoff_Gcal() \">"+date_array[j][i]+"</td>";
		}
		outStr += "</tr>\n";
	}
	outStr += "</table></td></tr></table>";

	
			

	//이전 다음달 링크 
	
	month_link = (month > 1)? "<a onClick=\"cal("+year+","+(month-1)+","+day+","+obj_name+",false)\" style='text-decoration:none; color:#909090; cursor:hand'>◁</a> <font color=black>"+month:"<a onClick=\"cal("+(year-1)+",12,"+day+","+obj_name+",false)\" style='text-decoration:none; color:#909090;cursor:hand'>◁</a> <font color=black>"+month;
	month_link += (month < 12)? "</font> <a onClick=\"cal("+year+","+(month+1)+","+day+","+obj_name+",false)\" style='text-decoration:none; color:#909090; cursor:hand'>▷</a>":"</font> <a onClick=\"cal("+(year+1)+",1,"+day+","+obj_name+",false)\" style='text-decoration:none; color:#909090; cursor:hand'>▷</a>";
	
	
	//HTML 대입
	document.all.G_cal_body.innerHTML = outStr;

	//Year 이전 다음 링크.
	document.all.G_cal_year.innerHTML = "<a onClick=\"cal("+(year-1)+","+month+","+day+","+obj_name+",false)\" style='text-decoration:none; color:#909090; cursor:hand'>◁</a> <font color=black>"+year+"</font> <a onClick=\"cal("+(year+1)+","+month+","+day+","+obj_name+",false)\" style='text-decoration:none; color:#909090; cursor:hand'>▷</a>";
	//Month 이전 다음 링크.
	document.all.G_cal_month.innerHTML = month_link;

	//만약 현재가 보이는 상태이면 div를 감춘다. 
	if(isOnOff == null || isOnOff == true)
		onoff_Gcal();
	
}

function onoff_Gcal(){
	
	/*
	document.all.G_cal.style.top = event.clientY+5;
	document.all.G_cal.style.left = event.clientX+5;	
	*/
	//alert(document.documentElement.scrollTop);
	document.all.G_cal.style.top = event.clientY+document.documentElement.scrollTop+5;
	document.all.G_cal.style.left = event.clientX+document.documentElement.scrollLeft+5;
	
	
	document.all.G_cal.style.visibility = (document.all.G_cal.style.visibility=="hidden")?"":"hidden";
}

document.writeln("<style>");
document.writeln("td.calendar{font-size:7pt; font-family:tahoma;color:black; background-color:white;text-align:center; }");
document.writeln("</style>");
document.writeln(""
+"<div id=G_cal style='width=160;position:absolute;visibility:hidden'>"
+"<table bgcolor=#EEEEEE  width=160 height=25 border=0 cellpadding=0 cellspacing=0 >"
+" <tr><td align=center style='font-size:8pt; font-family:tahoma'>"
+" <span id=G_cal_year></span>&nbsp;&nbsp;<span id=G_cal_month></span>"
+" </td></tr>"
+"</table>"
+"<div id=G_cal_body style='width:160'>"
+"</div></div>");
